<template>
  <div class="cart">
    <h4>全部商品</h4>

    <div class="cart-main">
      <div class="cart-th">
        <div class="cart-th1">全部</div>
        <div class="cart-th2">商品</div>
        <div class="cart-th3">单价（元）</div>
        <div class="cart-th4">数量</div>
        <div class="cart-th5">小计（元）</div>
        <div class="cart-th6">操作</div>
      </div>
      <div class="cart-body">
        <ul class="cart-list" v-for="(value, index) in cartInfoList" :key="value.id">
          <li class="cart-list-con1">
            <input type="checkbox" name="chk_list" 
              @change="updateChecked(value, $event)"
              :checked="value.isChecked"
            >
          </li>
          <li class="cart-list-con2">
            <img :src="value.imgUrl" />
            <div class="item-msg">{{value.skuName}}</div>
          </li>
          <li class="cart-list-con4">
            <span class="price">{{value.skuPrice}}.00</span>
          </li>

<!-- ------------------------------------------------------- -->          
          <!-- 加减 -->
          <li class="cart-list-con5">
            <a href="javascript:void(0)"class="mins" @click="handler('a', -1, value)">-</a>
            <input
              autocomplete="off"
              type="text"
              minnum="1"
              class="itxt"
              value="1" 
              :value="value.skuNum" 
              @change="handler('c', $event.target.value*1, value)" />
            <a href="javascript:void(0)" class="plus" @click="handler('b', 1, value)">+</a>
          </li>
<!-- ------------------------------------------------------- -->
          <li class="cart-list-con6">
            <span class="sum">{{value.skuNum * value.skuPrice}}</span>
          </li>
          <li class="cart-list-con7">
            <a href="#javascrip;" class="sindelet" @click="deleteCartById(value.skuId)">删除</a>
            <br />
            <a href="#none">移到收藏</a>
          </li>
        </ul>
      </div>
    </div>

    <div class="cart-tool">
      <!-- 总开关 -->
      <div class="select-all">
        <input class="chooseAll" type="checkbox" :checked=" true && cartInfoList.length!=0" @change="updateAllCart" />
        <span>全选</span>
      </div>
      <div class="option">
        <a @click="deleteAllCheckedCart">删除选中的商品</a>
        <a href="#none">移到我的关注</a>
        <a href="#none">清除下柜商品</a>
      </div>
      <div class="money-box">
        <div class="chosed">已选择 <span>0</span>件商品</div>
        <div class="sumprice">
          <em>总价（不含运费） ：</em>
          <i class="summoney">{{totalPrice}}</i>
        </div>
        <div class="sumbtn">
          <router-link class="sum-btn" to="/Trade">结算</router-link>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {mapGetters} from "vuex";
export default {
  name: "ShopCart",
  data() {
    return {
      time: null,
    }
  },
  computed: {
    ...mapGetters({cartList:"cartList"}),
    
    cartInfoList() {            
      return this.cartList ? this.cartList.cartInfoList : [];
    },
    
    totalPrice() {
      let sum = 0;
      
      this.cartInfoList.forEach((e) => {
        sum += (e.skuNum * e.skuPrice)-0;
      });     
      return sum;
    },

    // isAllCheck() {
    //   return this.cartInfoList.every(item => item.isChecked = 1)    
    // }
  },
  mounted() {
    this.$store.dispatch("reqList");
  },
  methods: {
    async handler(state, v, id) {    
      let i = 1;
      switch(state) {
        case "b":
          i = 1;
        break
        case "a":
          if(i < id.skuNum) { i = -1; }else { i = 0; }
        break
        case "c":
          if(isNaN(v) || v < 1) {
            i = 0;
          }else {
            i  = parseInt(v) - id.skuNum;
          }
        break
      } 

      try {
        await this.$store.dispatch("addOrUpdateShopCart", {skuId:id.skuId , skuNum:i});
        this.$store.dispatch("reqList");
      }catch(err) {
        console.log(err);
      }            
        
    },

    async deleteCartById(id) {
      try {
        await this.$store.dispatch("deleteCartById", id);
        await this.$store.dispatch("reqList");
      }catch(err) {
        console.log(err);
      }
    },

    async updateChecked(id, event) { 
     try{     
      await this.$store.dispatch("reqUpdateCheckedById", {skuId:id.skuId, isCheckef:event.target.checked?1:0});
      this.$store.dispatch("reqList");
     }catch(err) {
       alert("没网了。兄弟！")
     }
     
    },

    deleteAllCheckedCart() {
      this.$store.dispatch("deleteAllCheckedCart").then(value => {
        // console.log(value);
        this.$store.dispatch("reqList");
      }).catch(err => {
        console.log(err);
      });
      
      
    },

    // 全选勾选框
    async updateAllCart(event) {  
      let e = event.target.checked?1:0    
      this.cartInfoList.forEach(async (el, index) => {
        try{     
          await this.$store.dispatch("reqUpdateCheckedById", {skuId:el.skuId, isCheckef:e});
          await this.$store.dispatch("reqList"); 
        }catch(err) {
          alert("没网了。兄弟！")
        }
      });

      
    }
  }
}
</script>

<style scoped>
  .cart {
    width: 1200px;
    margin: 0 auto;
  }
  .cart h4 {
    margin: 9px 0;
    font-size: 14px;
    line-height: 21px;
  }
  .cart .cart-main .cart-th {
    background: #f5f5f5;
    border: 1px solid #ddd;
    padding: 10px;
    overflow: hidden;
  }
  .cart .cart-main .cart-th > div {
    float: left;
  }
  .cart .cart-main .cart-th .cart-th1 {
    width: 25%;
  }
  .cart .cart-main .cart-th .cart-th1 input {
    vertical-align: middle;
  }
  .cart .cart-main .cart-th .cart-th1 span {
    vertical-align: middle;
  }

  .cart .cart-main .cart-th .cart-th2 {
    width: 25%;
  }
  .cart .cart-main .cart-th .cart-th3,
  .cart-th4,
  .cart-th5,
  .cart-th6 {
    width: 12.5%;
  }

  .cart .cart-main .cart-body {
    margin: 15px 0;
    border: 1px solid #ddd;
  }
  .cart .cart-main .cart-body .cart-list {
    padding: 10px;
    border-bottom: 1px solid #ddd;
    overflow: hidden;
  }
  .cart .cart-main .cart-body .cart-list > li {
    float: left;
  }
  .cart .cart-main .cart-body .cart-list .cart-list-con1 {
    width: 15%;
  }
  .cart .cart-main .cart-body .cart-list .cart-list-con2 {
    width: 35%;
  }
  .cart .cart-main .cart-body .cart-list .cart-list-con2 img {
    width: 82px;
    height: 82px;
    float: left;
  }
  .cart .cart-main .cart-body .cart-list .cart-list-con2 .item-msg {
    float: left;
    width: 150px;
    margin: 0 10px;
    line-height: 18px;
  }

  .cart .cart-main .cart-body .cart-list .cart-list-con4 {
    width: 10%;
  }
  .cart .cart-main .cart-body .cart-list .cart-list-con5 {
    width: 17%;
  }
  .cart .cart-main .cart-body .cart-list .cart-list-con5 .mins {
    border: 1px solid #ddd;
    border-right: 0;
    float: left;
    color: #666;
    width: 6px;
    text-align: center;
    padding: 8px;
  }
  .cart .cart-main .cart-body .cart-list .cart-list-con5 input {
    border: 1px solid #ddd;
    width: 40px;
    height: 33px;
    float: left;
    text-align: center;
    font-size: 14px;
  }
  .cart .cart-main .cart-body .cart-list .cart-list-con5 .plus {
    border: 1px solid #ddd;
    border-left: 0;
    float: left;
    color: #666;
    width: 6px;
    text-align: center;
    padding: 8px;
  }
  .cart .cart-main .cart-body .cart-list .cart-list-con6 {
    width: 10%;
  }
  .cart .cart-main .cart-body .cart-list .cart-list-con6 .sum {
    font-size: 16px;
  }
  .cart .cart-main .cart-body .cart-list .cart-list-con7 {
    width: 13%;
  }
  .cart .cart-main .cart-body .cart-list .cart-list-con7 a {
    color: #666;
  }




  .cart .cart-tool {
    overflow: hidden;
    border: 1px solid #ddd;
  }
  .cart .cart-tool .select-all {
    padding: 10px;
    overflow: hidden;
    float: left;
  }
  .cart .cart-tool .select-all span {
    vertical-align: middle;
  }
  .cart .cart-tool .select-all input {
    vertical-align: middle;
  }
  .cart .cart-tool .option {
    padding: 10px;
    overflow: hidden;
    float: left;
  }
  .cart .cart-tool .option a {
    float: left;
    padding: 0 10px;
    color: #666;
  }

  .cart .cart-tool .money-box {
    float: right;
  }
  .cart .cart-tool .money-box .chosed {
    line-height: 26px;
    float: left;
    padding: 0 10px;
  }
  .cart .cart-tool .money-box .sumprice {
    width: 200px;
    line-height: 22px;
    float: left;
    padding: 0 10px;
  }
  .cart .cart-tool .money-box .sumprice .summoney {
    color: #c81623;
    font-size: 16px;
  }

  .cart .cart-tool .money-box .sumbtn {
    float: right;
  }
  .cart .cart-tool .money-box .sumbtn a {
    display: block;
    position: relative;
    width: 96px;
    height: 52px;
    line-height: 52px;
    color: #fff;
    text-align: center;
    font-size: 18px;
    font-family: "Microsoft YaHei";
    background: #e1251b;
    overflow: hidden;
  }
</style>